<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="货币" prop="moneyId">
        <el-select v-model="queryParams.moneyId" placeholder="请选择" clearable @change="handleQuery">
          <el-option v-for="item in moneyList" :key="item.id" :label="item.moneyName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收支" prop="payment">
        <el-select v-model="queryParams.payment" placeholder="请选择" clearable @change="handleQuery">
          <el-option v-for="item in paymentOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="来源" prop="useRemark">
        <el-input v-model="queryParams.useRemark" placeholder="请输入来源" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['system:incomeLog:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="incomeLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="货币" align="center" prop="moneyName" />
      <el-table-column label="收支" align="center">
        <template #default="scope">
          {{ scope.row.payment == 1 ? '收入' : '支出' }}
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="余额" align="center" prop="balance" />
      <el-table-column label="来源/用途简介" align="center" prop="useRemark" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup name="IncomeLog">
import { listIncomeLog } from "@/api/system/incomeLog";

const { proxy } = getCurrentInstance();

import { listMoney } from "@/api/money/money";


const incomeLogList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const route = useRoute();
const moneyList = ref([])
const paymentOptions = ref([
  {
    label: "收入",
    value: 1
  },
  {
    label: "支出",
    value: 2
  },
])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    moneyId: null,
    payment: null,
    amount: null,
    balance: null,
    useRemark: null,
    useSource: null,
    createTime: null,
    createBy: null
  },
});

const { queryParams, form } = toRefs(data);

function getMoneyList() {
  listMoney({
    pageNum: 1,
    pageSize: 9999
  }).then(res => {
    moneyList.value = res.rows || []
  })
}

getMoneyList()

/** 查询用户收支明细总列表 */
function getList() {
  loading.value = true;
  queryParams.value.createBy = route.query.id
  listIncomeLog(queryParams.value).then(response => {
    incomeLogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/incomeLog/export', {
    ...queryParams.value
  }, `incomeLog_${new Date().getTime()}.xlsx`)
}

getList();
</script>
